↜ Back to index Introduction to Numerical Analysis 2

Lecture 5

Two dimensional arrays

An m\times n-matrix is a table of mn numbers indexed by numbers. In Fortran, matrices can be stored using two dimensional arrays.

\begin{pmatrix} a_{11} & a_{12} &&\cdots & a_{1n}\\ a_{21} & a_{22} & a_{23} && \\ a_{31} & a_{32} & a_{33} & \\ \vdots& & & \ddots &\vdots\\ a_{m1} & a_{m2} && \cdots & a_{mn}\\ \end{pmatrix}

We can specify a given element by giving numbers 1 \leq i \leq m and 1 \leq j \leq n.

In Fortran, we can use the following code to declare a two-dimensional array:

implicit none

real a(3,2)         ! array of size 3 x 2

integer, parameter :: m = 3, n = 2
real b(m, n)        ! the same, but with constants m, n

integer i, j        ! for do loops


a(1, 2) = 5.        ! assign 5. to element a(1, 2)

print *, a(1, 2)    ! print content of element a(1, 2)

a = 1               ! initialize all elements to 0

! we can also use do loops to initialize
do i = 1, m
    do j =1, n
        a(i, j) = 10 * i + j 
    end do
end do

! this prints the matrix nicely
print *,"a ="
do i = 1, m
    print *, a(i, :)
end do

! this is not so nice
print *,"a (not nice) ="
print *, a

b = 2 * a           ! set all elements of b to 2 * the value of elements in a

print *,"b ="
do i = 1, m
    print *, b(i, :)
end do

! the same thing using do loops
do i = 1, m
    do j =1, n
        b(i, j) = 2 * a(i, j)
    end do
end do

end

Note that we print the matrix using the following code:

do i = 1, m
    print *, a(i, :)
end do

This prints the values of the elements in the order that the matrix elements are written in math.

We can use : to access rows and columns of a 2-dimensional array as an 1-dimensional array.

To read a matrix form the input in the “natural” order: first row on the first line, 2nd on the second line etc., use

do i = 1, m
    read *, a(i, :)
end do

Allocatable arrays

Two dimensional arrays can also be allocatable.

implicit none
integer, dimension(:, :), allocatable :: a
integer m, n, i, j

print *, 'Enter matrix size: m, n'
read *, m, n

! allocate the memory
allocate(a(m, n))

do i = 1, m
    do j = 1, n
        a(i, j) = 10 * i + j
    enddo
enddo

do i = 1, m
    print *, a(i, :)
enddo

end

Exercises

In the following exercises, use integer typed allocatable two dimensional arrays, and perform the operations using do loops.

To print the elements of the resulting arrays, use code like:

do i = 1, m
    print *, a(i, :)
end do

Exercise 1. Write a code that reads n, initializes a n\times n array a with elements of the identity matrix (単位行列), and prints its elements to the output.

a_{ij} = \begin{cases} 1, & i = j,\\ 0, & \text{otherwise}. \end{cases}

Exercise 2. Write a code that reads n, initializes a n\times n array b with elements of the tridiagonal matrix (三対角行列). The elements on the main diagonal are 1, on the diagonal above the main diagonal are 2 and below the main diagonal are 3. Then it prints the elements to the screen.

b_{ij} = \begin{cases} 1, & i = j,\\ 2, & j = i + 1,\\ 3, & j = i - 1,\\ 0, & \text{otherwise}. \end{cases}

Exercise 3. Write a code that reads n, initializes a n\times n array c with elements of the upper triangular matrix from the last lecture (report) with elements 2 on the diagonal and 3 above the diagonal, and then prints the elements to the screen.

c_{ij} = \begin{cases} 2, & i = j,\\ 3, & j > i,\\ 0, & \text{otherwise}. \end{cases}

Exercise 4. Write a code that read n, computes the sum of each row of the matrix in the array c of the previous exercise and stores it in an array r of n elements. Then it prints the elements r to the screen.

r_i = \sum_{j=1}^n a_{ij}

Exercise 5. Write a code that given a matrix A stored in an array a of given size n \times n, and two integers i, and j, 1 \leq i, j \leq n, adds the row i of the matrix A to the row j of A (elementwise).

To test your code, you can use the array c from Exercise 3.

Given n = 3, i = 1 and j = 3 and the matrix A \begin{pmatrix} 1 & 2 & 3\\ 0 & 1 & 2\\ -1 & -3 & 5 \end{pmatrix} in the array a, it ends up with array a that has entries corresponding to the matrix \begin{pmatrix} 1 & 2 & 3\\ 0 & 1 & 2\\ 0 & -1 & 8 \end{pmatrix}

Exercise 6. Modify the program in Exercise 5 so that it reads n, the values in a (values in the first row entered first, then the second row and so on) and finally i and j from the keyboard input. The values of the resulting matrix are then printed to the output.

Exercise 7. Write a code that reads n, the values of a n \times n matrix (first row first, then 2nd, etc.) and checks whether the matrix is symmetric. It prints yes if it is, otherwise it prints no.

Exercise 8. Write a code that reads 3 positive integers m, n, p, the values of a m \times n matrix (first row first, then 2nd, etc.) A, the values of n \times p matrix B in the same way, and computes the elements of matrix C that is the product AB of A and B. It then prints the elements C to the screen.

c_{ik} = \sum_{j = 1}^n a_{ij} b_{jk}